package com.cdc.video;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.cdc.videodemo.BaseActivity;
import com.cdc.videodemo.R;
import com.pili.pldroid.player.AVOptions;
import com.pili.pldroid.player.widget.PLVideoView;
import com.pili.pldroid.playerdemo.widget.CopyOfMediaController;
import com.pili.pldroid.playerdemo.widget.MediaController;

public class VideoActivity1 extends BaseActivity {

	private PLVideoView videoView;

	private CopyOfMediaController mMediaController;

	private boolean mIsActivityPaused = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_1);

		videoView = (PLVideoView) findViewById(R.id.VideoView);

		View loadingView = findViewById(R.id.LoadingView);
		videoView.setBufferingIndicator(loadingView);

		AVOptions options = new AVOptions();

		int isLiveStreaming = 1;
		// the unit of timeout is ms
		options.setInteger(AVOptions.KEY_PREPARE_TIMEOUT, 10 * 1000);
		options.setInteger(AVOptions.KEY_GET_AV_FRAME_TIMEOUT, 10 * 1000);
		// Some optimization with buffering mechanism when be set to 1
		options.setInteger(AVOptions.KEY_LIVE_STREAMING, isLiveStreaming);
		if (isLiveStreaming == 1) {
			options.setInteger(AVOptions.KEY_DELAY_OPTIMIZATION, 1);
		}

		// 1 -> hw codec enable, 0 -> disable [recommended]
		int codec = 0;
		options.setInteger(AVOptions.KEY_MEDIACODEC, codec);

		// whether start play automatically after prepared, default value is 1
		options.setInteger(AVOptions.KEY_START_ON_PREPARED, 0);

		videoView.setAVOptions(options);

		videoView.setVideoPath(FLV);

		// You can also use a custom `MediaController` widget
		mMediaController = new CopyOfMediaController(this, true,
				isLiveStreaming == 1);
		videoView.setMediaController(mMediaController);

	}

	@Override
	protected void onResume() {
		super.onResume();
		mIsActivityPaused = false;
		videoView.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mIsActivityPaused = true;
		videoView.pause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		videoView.stopPlayback();
	}

}
